package dao.daocontrollers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import api.entitites.IGarage;
import api.entitites.IMaster;
import api.entitites.IOrder;
import api.entitites.OrderStatus;
import api.exportimportentities.IExportImportEntities;
import api.messages.Messager;
import api.workerswithdb.IDAOGarage;
import api.workerswithdb.IDAOMaster;
import api.workerswithdb.IDAOOrder;
import api.workerswithdb.IDAOWorker;
import api.workerswithdb.SortMastersBy;
import api.workerswithdb.SortOrdersBy;
import autoproperties.AutoProperties;
import entitiesfactory.EntitiesFactory;
import exportimporterentities.ExportImportEntities;

public class DAOWorker implements IDAOWorker {
	private static final String DELIMITER = " ";
	private static final String DATE_FORMAT = "yyyy-MM-dd";
	private SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
	private IDAOGarage daoGarage;
	private IDAOOrder daoOrder;
	private IDAOMaster daoMaster;
	private EntitiesFactory entitiesFactory = EntitiesFactory.getInstance();
	private AutoProperties autoProperties;
	private ArrayList<String> answer = new ArrayList<String>();
	private IExportImportEntities exportImpotEntities;

	public DAOWorker() {
		daoGarage = new DAOGarage();
		daoOrder = new DAOOrder();
		daoMaster = new DAOMaster();
		autoProperties = AutoProperties.getInstance();
		exportImpotEntities = new ExportImportEntities();
	}

	public ArrayList<String> getEmptyGarage() {
		answer.clear();
		Integer places = daoGarage.getEmptyGarage();
		if (places == 0) {
			answer.add(DBMessanger.ALL_GARAGES_BUSY);
		} else if (places == null) {
			answer.add(DBMessanger.ERROR);
		} else {
			answer.add(DBMessanger.NUMBER_GARAGE + places);
		}
		return answer;
	}

	public ArrayList<String> getOrdersByMaster(String masterName) {
		answer.clear();
		Integer numberOrder = daoMaster.getOrderByMaster(masterName);
		if (numberOrder != 0) {
			answer.add(numberOrder.toString());
		} else {
			answer.add(DBMessanger.ERROR_ORDER_BY_MASTER);
		}
		return answer;
	}

	public ArrayList<String> getMastersByOrder(Integer numberOrder) {
		answer.clear();
		String masterName = daoOrder.getMasterByOrder(numberOrder);
		if (!masterName.equals(null)) {
			answer.add(masterName);
		} else {
			answer.add(DBMessanger.ORD_NOT_EXISTS);
		}
		return answer;
	}

	public ArrayList<String> getEmptyPlacesOnDate(Date date) {
		answer.clear();
		String result = daoGarage.getEmptyPlacesOnDate(date);
		String arrayTemp[] = new String[2];
		arrayTemp = result.split(DELIMITER);
		if (arrayTemp[0] != null && arrayTemp[1] != null) {
			Integer masterCount = Integer.parseInt(arrayTemp[0]);
			Integer garageCount = Integer.parseInt(arrayTemp[1]);
			if (garageCount == 0 || masterCount == 0) {
				answer.add(DBMessanger.NOT_EMPTY_PLACE_ON_DATE);
			} else if (masterCount > garageCount) {
				answer.add(DBMessanger.NUMBER_GARAGE_ON_DATE + garageCount);
			} else if (masterCount < garageCount) {
				answer.add(DBMessanger.NUMBER_GARAGE_ON_DATE + masterCount);
			} else {
				answer.add(DBMessanger.NUMBER_GARAGE_ON_DATE + garageCount);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> getNextFreeDate() {
		answer.clear();
		Date date = daoOrder.getNextFreeDate();
		if (date == null) {
			answer.add(DBMessanger.ERROR);
		} else {
			answer.add(DBMessanger.NEXT_FREE_DATE + formatter.format(date));
		}
		return answer;
	}

	public ArrayList<String> SortOrderByBeginningDate() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortAllOrders(SortOrdersBy.BEGINING_DATE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortOrderByDeadlineDate() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortAllOrders(SortOrdersBy.DEADLINE_DATE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortOrderByFillingDate() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortAllOrders(SortOrdersBy.FILLING_DATE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortOrderByPrice() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortAllOrders(SortOrdersBy.PRICE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortMasterByEmployment() {
		answer.clear();
		ArrayList<IMaster> masterList = new ArrayList<IMaster>();
		masterList.addAll(daoMaster
				.SortMasters(SortMastersBy.EMPLOYMENT_MASTER));
		if (masterList.get(0) == null) {
			answer.add(DBMessanger.ERROR);
		} else {
			for (IMaster master : masterList) {
				String line = DBMessanger.MASTERS_NAME + master.getNameMaster()
						+ DBMessanger.MASTERS_EMPLOYMENT
						+ master.getEmploymentMaster();
				answer.add(line);
			}
		}
		return answer;

	}

	public ArrayList<String> SortMasterByName() {
		answer.clear();
		ArrayList<IMaster> masterList = new ArrayList<IMaster>();
		masterList.addAll(daoMaster.SortMasters(SortMastersBy.MASTER_NAME));
		if (masterList.get(0) == null) {
			answer.add(DBMessanger.ERROR);
		} else {
			for (IMaster master : masterList) {
				String line = DBMessanger.MASTERS_NAME + master.getNameMaster()
						+ DBMessanger.MASTERS_EMPLOYMENT
						+ master.getEmploymentMaster();
				answer.add(line);
			}
		}
		return answer;
	}

	public ArrayList<String> SortMadingOrderByBeginningDate() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortMadingOrders(SortOrdersBy.BEGINING_DATE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortMadingOrderByDeadlineDate() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortMadingOrders(SortOrdersBy.DEADLINE_DATE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortMadingOrderByFillinggDate() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortMadingOrders(SortOrdersBy.FILLING_DATE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> SortMadingOrderByPrice() {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortMadingOrders(SortOrdersBy.PRICE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortDeletedOrderByPeriodByBeginningDate(
			Date begin, Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		try {
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.BEGINING_DATE, OrderStatus.DELETED));
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} catch(Exception e){
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortCanceledOrderByPeriodByBeginningDate(
			Date begin, Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.BEGINING_DATE, OrderStatus.CANCELED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortMadeOrderByPeriodByBeginningDate(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.BEGINING_DATE, OrderStatus.MADE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortDeletedOrderByPeriodByDeadlineDate(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.DEADLINE_DATE, OrderStatus.DELETED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortCanceledOrderByPeriodByDeadlineDate(
			Date begin, Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.DEADLINE_DATE, OrderStatus.CANCELED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortMadeOrderByPeriodByDeadlineDate(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.DEADLINE_DATE, OrderStatus.MADE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortDeletedOrderByPeriodByFillingDate(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.FILLING_DATE, OrderStatus.DELETED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortCanceledOrderByPeriodByFillingDate(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.FILLING_DATE, OrderStatus.CANCELED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortMadeOrderByPeriodByFillingDate(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.FILLING_DATE, OrderStatus.MADE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortDeletedOrderByPeriodByPrice(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.PRICE, OrderStatus.DELETED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortCanceledOrderByPeriodByPrice(Date begin,
			Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.PRICE, OrderStatus.CANCELED));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> SortMadeOrderByPeriodByPrice(Date begin, Date end) {
		answer.clear();
		ArrayList<IOrder> listOrder = new ArrayList<IOrder>();
		listOrder.addAll(daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.PRICE, OrderStatus.MADE));
		if (listOrder.get(0) != null) {
			for (IOrder order : listOrder) {
				String line = DBMessanger.ORDER_NUMBER + order.getNumberOrder()
						+ DBMessanger.ORDER_STATUS + order.getOrderStatus()
						+ DBMessanger.ORDER_BEGINING_DATE
						+ formatter.format(order.getBeginingDate())
						+ DBMessanger.ORDER_FILLING_DATE
						+ formatter.format(order.getFillingDate())
						+ DBMessanger.ORDER_DEADLINE_DATE
						+ formatter.format(order.getDeadlineDate())
						+ DBMessanger.ORDER_PRICE + order.getPrice();
				answer.add(line);
			}
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> addMaster(String masterName) {
		answer.clear();
		IMaster master = (IMaster) entitiesFactory.getObject(IMaster.class);
		master.setNameMaster(masterName);
		Boolean result = daoMaster.insertMasterToDB(master);
		if (result) {
			answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
		} else {
			answer.add(DBMessanger.MASTER_EXISTS);
		}
		return answer;

	}

	public ArrayList<String> deleteMaster(String masterName) {
		answer.clear();
		Boolean result = daoMaster.deleteMaster(masterName);
		if (result) {
			answer.add(DBMessanger.MASTER_DELETE);
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> addGarage(Integer garageNumber) {
		answer.clear();
		if (autoProperties.getGarageManager() == true) {
			IGarage garage = (IGarage) entitiesFactory.getObject(IGarage.class);
			garage.setNumberGarage(garageNumber);
			Boolean result = daoGarage.insertGarageToDB(garage);
			if (result) {
				answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				answer.add(DBMessanger.GARAGE_EXISTS);
			}
			return answer;
		} else {
			answer.clear();
			answer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return answer;
		}

	}

	public ArrayList<String> deleteGarage(Integer numberGarage) {
		answer.clear();
		if (autoProperties.getGarageManager()) {
			Boolean result = daoGarage.deleteGarage(numberGarage);
			if (result) {
				answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				answer.add(DBMessanger.GARAGE_DELETE);
			}
			return answer;

		} else {
			answer.clear();
			answer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return answer;
		}
	}

	public ArrayList<String> addOrder(Integer orderNumber, Date fillingDate,
			Date deadlineDate, Date beginingDate, Integer price,
			String masterName, Integer numberGarage) {
		answer.clear();
		IOrder order = (IOrder) entitiesFactory.getObject(IOrder.class);
		order.setNumberOrder(orderNumber);
		order.setFillingDate(fillingDate);
		order.setDeadlineDate(deadlineDate);
		order.setBeginingDate(beginingDate);
		order.setPrice(price);
		Integer id_master = daoMaster.getIdMaster(masterName);
		Integer id_garage = daoGarage.getIdGarage(numberGarage);
		order.sendMasterGarage(id_master, id_garage);
		Boolean result = daoOrder.insertOrderToDB(order);
		if (result) {
			answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
		} else {
			answer.add(DBMessanger.ORD_EXISTS);
		}
		return answer;

	}

	public ArrayList<String> markOrderDeleted(Integer numberOrder) {
		answer.clear();
		if (autoProperties.getPermissionDeleteOrder() == true) {
			Boolean result = daoOrder.markOrder(numberOrder,
					OrderStatus.DELETED);
			if (result) {
				answer.add(DBMessanger.ORDER_STATUS_CHANGE);
			} else {
				answer.add(DBMessanger.ERROR);
			}
			return answer;
		} else {
			answer.clear();
			answer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return answer;

		}
	}

	public ArrayList<String> markOrderCanceled(Integer numberOrder) {
		answer.clear();
		Boolean result = daoOrder.markOrder(numberOrder, OrderStatus.CANCELED);
		if (result) {
			answer.add(DBMessanger.ORDER_STATUS_CHANGE);
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;

	}

	public ArrayList<String> markOrderMade(Integer numberOrder) {
		answer.clear();
		Boolean result = daoOrder.markOrder(numberOrder, OrderStatus.MADE);
		if (result) {
			answer.add(DBMessanger.ORDER_STATUS_CHANGE);
		} else {
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

	public ArrayList<String> ShiftTimeOrder(Date newDate, Integer numberOrder) {
		answer.clear();
		if (autoProperties.getPermissionShiftTimeOrder()) {
			Boolean result = daoOrder.shiftDeadlineDate(newDate, numberOrder);
			if (result) {
				answer.add(DBMessanger.ORDER_DATE_CHANGE);
			} else {
				answer.add(DBMessanger.ERROR);
			}
			return answer;
		} else {
			answer.clear();
			answer.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return answer;
		}
	}

	public ArrayList<String> cloneOrder(Integer numberOrder) {
		answer.clear();
		Integer result = daoOrder.cloneOrder(numberOrder);
		if (result == 0) {
			answer.add(DBMessanger.EXCEPTION_TRANSACTION);
		} else {
			answer.add(DBMessanger.CLONE + result);
		}
		return answer;
	}

	public ArrayList<String> exportGarages(Integer numberGarage) {
		IGarage garage = daoGarage.getGarageFromDb(numberGarage);
		if (garage != null) {
			return exportImpotEntities.ExportGarages(garage);
		} else {
			answer.clear();
			answer.add(Messager.GARAGE_NOT_EXISTS);
			return answer;
		}
	}

	public ArrayList<String> exportMasters(String name_master) {
		IMaster master = daoMaster.getMasterFromDb(name_master);
		if (master != null) {
			return exportImpotEntities.ExportMasters(master);
		} else {
			answer.clear();
			answer.add(Messager.MAS_NOT_EXISTS);
			return answer;

		}
	}

	public ArrayList<String> exportOrders(Integer number_order) {
		IOrder order = daoOrder.getOrderFromDb(number_order);
		if (order != null) {
			return exportImpotEntities.ExportOrders(order);
		} else {
			answer.clear();
			answer.add(Messager.ORD_NOT_EXISTS);
			return answer;

		}
	}

	public ArrayList<String> importGarages() {
		answer.clear();
		try {
			Boolean result = false;
			for (IGarage gar : exportImpotEntities.ImportGarages()) {
				answer.clear();
				result = daoGarage.insertGarageToDB(gar);
			}
			if (result) {
				answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				answer.add(DBMessanger.ERROR);
			}
			return answer;
		} catch (Exception e) {
			answer.clear();
			answer.add(Messager.ERROR);
			return answer;
		}

	}

	public ArrayList<String> importMasters() {
		answer.clear();
		Boolean result = false;
		try {
			for (IMaster mas : exportImpotEntities.ImportMasters()) {
				result = daoMaster.insertMasterToDB(mas);
			}
			if (result) {
				answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				answer.add(DBMessanger.ERROR);
			}
			return answer;

		} catch (Exception e) {
			answer.clear();
			answer.add(Messager.ERROR);
			return answer;
		}

	}

	public ArrayList<String> importOrders() {
		answer.clear();
		Boolean result = false;
		try {
			for (IOrder ord : exportImpotEntities.ImportOrders()) {
				result = daoOrder.insertOrderToDB(ord);
			}
			if (result) {
				answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);
			} else {
				answer.add(DBMessanger.ERROR);
			}
			return answer;

		} catch (Exception e) {
			answer.clear();
			answer.add(Messager.ERROR);
			return answer;
		}
	}

}
